﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SmsLog
    {
        public int Add(Chain.Model.SmsLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SmsLog(");
            builder.Append("SmsMemID,SmsMobile,SmsContent,SmsShopID,SmsTime,SmsUserID,SmsAmount,SmsAllAmount)");
            builder.Append(" values (");
            builder.Append("@SmsMemID,@SmsMobile,@SmsContent,@SmsShopID,@SmsTime,@SmsUserID,@SmsAmount,@SmsAllAmount)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SmsMemID", SqlDbType.Int, 4), new SqlParameter("@SmsMobile", SqlDbType.VarChar, 50), new SqlParameter("@SmsContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@SmsShopID", SqlDbType.Int, 4), new SqlParameter("@SmsTime", SqlDbType.DateTime), new SqlParameter("@SmsUserID", SqlDbType.Int, 4), new SqlParameter("@SmsAmount", SqlDbType.Int, 4), new SqlParameter("@SmsAllAmount", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.SmsMemID;
            cmdParms[1].Value = model.SmsMobile;
            cmdParms[2].Value = model.SmsContent;
            cmdParms[3].Value = model.SmsShopID;
            cmdParms[4].Value = model.SmsTime;
            cmdParms[5].Value = model.SmsUserID;
            cmdParms[6].Value = model.SmsAmount;
            cmdParms[7].Value = model.SmsAllAmount;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int SmsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SmsLog ");
            builder.Append(" where SmsID=@SmsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SmsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SmsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string SmsIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SmsLog ");
            builder.Append(" where SmsID in (" + SmsIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int SmsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SmsLog");
            builder.Append(" where SmsID=@SmsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SmsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SmsID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SmsID,SmsMemID,SmsMobile,SmsContent,SmsShopID,SmsTime,SmsUserID,SmsAmount,SmsAllAmount ");
            builder.Append(" FROM SmsLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" SmsID,SmsMemID,SmsMobile,SmsContent,SmsShopID,SmsTime,SmsUserID,SmsAmount,SmsAllAmount ");
            builder.Append(" FROM SmsLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.SmsID desc");
            }
            builder.Append(")AS Row, T.*  from SmsLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SysShop,SmsLog,SysUser";
            string[] column = new string[] { "SysShop.ShopName,SmsLog.*,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "SmsID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("SmsID", "SmsLog");
        }

        public Chain.Model.SmsLog GetModel(int SmsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 SmsID,SmsMemID,SmsMobile,SmsContent,SmsShopID,SmsTime,SmsUserID,SmsAmount,SmsAllAmount from SmsLog ");
            builder.Append(" where SmsID=@SmsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SmsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SmsID;
            Chain.Model.SmsLog log = new Chain.Model.SmsLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["SmsID"] != null) && (set.Tables[0].Rows[0]["SmsID"].ToString() != ""))
            {
                log.SmsID = int.Parse(set.Tables[0].Rows[0]["SmsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SmsMemID"] != null) && (set.Tables[0].Rows[0]["SmsMemID"].ToString() != ""))
            {
                log.SmsMemID = int.Parse(set.Tables[0].Rows[0]["SmsMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SmsMobile"] != null) && (set.Tables[0].Rows[0]["SmsMobile"].ToString() != ""))
            {
                log.SmsMobile = set.Tables[0].Rows[0]["SmsMobile"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SmsContent"] != null) && (set.Tables[0].Rows[0]["SmsContent"].ToString() != ""))
            {
                log.SmsContent = set.Tables[0].Rows[0]["SmsContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SmsShopID"] != null) && (set.Tables[0].Rows[0]["SmsShopID"].ToString() != ""))
            {
                log.SmsShopID = int.Parse(set.Tables[0].Rows[0]["SmsShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SmsTime"] != null) && (set.Tables[0].Rows[0]["SmsTime"].ToString() != ""))
            {
                log.SmsTime = DateTime.Parse(set.Tables[0].Rows[0]["SmsTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SmsUserID"] != null) && (set.Tables[0].Rows[0]["SmsUserID"].ToString() != ""))
            {
                log.SmsUserID = int.Parse(set.Tables[0].Rows[0]["SmsUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SmsAmount"] != null) && (set.Tables[0].Rows[0]["SmsAmount"].ToString() != ""))
            {
                log.SmsAmount = int.Parse(set.Tables[0].Rows[0]["SmsAmount"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SmsAllAmount"] != null) && (set.Tables[0].Rows[0]["SmsAllAmount"].ToString() != ""))
            {
                log.SmsAllAmount = int.Parse(set.Tables[0].Rows[0]["SmsAllAmount"].ToString());
            }
            return log;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SmsLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int GetSmsMonthNumber(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(SmsMonthNumber) FROM V_SmsShopReport ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetSmsShopReport(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_SmsShopReport";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ShopID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetSmsShopReportDetail(string strShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ShopID,ShopName,SubString(Convert(varchar(30), SmsTime,23),0,8) as SmsYearMonth,sum(SmsAllAmount) AS SmsNumber");
            builder.Append(" FROM SmsLog,SysShop");
            builder.Append(" WHERE SysShop.ShopID = SmsLog.SmsShopID AND SmsShopID =" + strShopID);
            builder.Append(" GROUP BY ShopID,ShopName,SubString(Convert(varchar(30), SmsTime,23),0,8) ORDER BY SubString(Convert(varchar(30), SmsTime,23),0,8) DESC");
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetSmsTotalNumber(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(SmsTotalNumber) FROM V_SmsShopReport ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SmsLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SmsLog set ");
            builder.Append("SmsMemID=@SmsMemID,");
            builder.Append("SmsMobile=@SmsMobile,");
            builder.Append("SmsContent=@SmsContent,");
            builder.Append("SmsShopID=@SmsShopID,");
            builder.Append("SmsTime=@SmsTime,");
            builder.Append("SmsUserID=@SmsUserID,");
            builder.Append("SmsAmount=@SmsAmount,");
            builder.Append("SmsAllAmount=@SmsAllAmount");
            builder.Append(" where SmsID=@SmsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SmsMemID", SqlDbType.Int, 4), new SqlParameter("@SmsMobile", SqlDbType.VarChar, 50), new SqlParameter("@SmsContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@SmsShopID", SqlDbType.Int, 4), new SqlParameter("@SmsTime", SqlDbType.DateTime), new SqlParameter("@SmsUserID", SqlDbType.Int, 4), new SqlParameter("@SmsAmount", SqlDbType.Int, 4), new SqlParameter("@SmsAllAmount", SqlDbType.Int, 4), new SqlParameter("@SmsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.SmsMemID;
            cmdParms[1].Value = model.SmsMobile;
            cmdParms[2].Value = model.SmsContent;
            cmdParms[3].Value = model.SmsShopID;
            cmdParms[4].Value = model.SmsTime;
            cmdParms[5].Value = model.SmsUserID;
            cmdParms[6].Value = model.SmsAmount;
            cmdParms[7].Value = model.SmsAllAmount;
            cmdParms[8].Value = model.SmsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

